#!/usr/bin/env python
# -*- coding: utf-8 -*-

# http://apiwiki.twitter.com/Twitter-REST-API-Method%3A-account%C2%A0update_profile
# curl -u user:password -d "url=http://www.igudo.com" http://twitter.com/account/update_profile.xml

# limitations to urllib:  cannot specify authenticating proxies, 
# must use http_proxy environment variable before invoking the script

from urllib import urlopen, urlencode
from random import seed, randint
from config import username, password, titles

numTitles = len(titles)
numbersPicked = []
done = False
chosenTitle = ""

# randomly choose some titles until we get close to filling up 160 characters
while not done:
	titleToPick = randint(0,numTitles-1)
	if not titleToPick in numbersPicked:
		if len(chosenTitle) + len(titles[titleToPick]) + 3 <= 160:
			numbersPicked.append(titleToPick)
			if len(chosenTitle) > 0:
				chosenTitle += ' | '
			chosenTitle += titles[titleToPick]
		else:
			done = True

params = urlencode({'description': chosenTitle})

print urlopen( 'http://' + username + ':' + password + '@twitter.com/account/update_profile.xml', params).read()
